const {createStatementData} = require('./createStatementData_v10.js')
// 引用方式：CommonJS
const invoices = require("../asset/invoices.json")
const plays = require("../asset/plays.json")


/**
 * 核心程序:拆分阶段（1.生成中转数据结构； 2.将数据渲染成文本或 HTML）
 * @param {*} invoice 
 * @param {*} plays 
 * @returns 
 */
function statement(invoice, plays) {
  // 
  return renderPlainText(createStatementData(invoice, plays));
}

/**
 * 打印账单详情
 * @param {*} invoice 
 * @param {*} plays 
 * @returns 
 */
function renderPlainText(data) {
  let result = `Statement for ${data.customer}\n`;
  for (let perf of data.performances) {
    result += ` ${perf.play.name}: ${usd(perf.amount)} (${perf.audience} seats)\n`;
  }
  result += `Amount owed is ${usd(data.totalAmount)}\n`;
  result += `You earned ${data.totalVolumeCredits} credits\n`;
  return result;



  /**
   * 将数字格式化为 货币 & 将美分转换为美元（除以100）
   * @param {*} aNumber 
   */
  function usd(aNumber) {
    return new Intl.NumberFormat("en-US",
      {
        style: "currency", currency: "USD",
        minimumFractionDigits: 2
      }).format(aNumber / 100);
  }





}




// 调用
let invoice = invoices[0];
res = statement(invoice, plays);
console.log(res);
